package com.booza.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Common DAO functions.
 */
public abstract class CommonDAOImpl implements CommonDAO
{
	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * Find object by ID.
	 * 
	 * @param class
	 * @param pk
	 */
	public Object findObjectById(Class<?> class1, Serializable pk)
	{
		return sessionFactory.getCurrentSession().get(class1, pk);
	}

	/**
	 * Executes a generic query.
	 * 
	 * @param query
	 *          -query to be executed
	 * @param colName
	 *          - column in the criteria
	 * @param val
	 *          - value to be applied
	 * @return Return results matching the criteria.
	 */
	public Collection<?> executeQuery(String query, String colName, Object val)
	{
		return (List<?>) sessionFactory.getCurrentSession().createQuery(query).setParameter(colName, val).list();
	}

	public void saveOrUpdate(Object o)
	{
		sessionFactory.getCurrentSession().saveOrUpdate(o);
	}

	public void delete(Object o)
	{
		sessionFactory.getCurrentSession().delete(o);
	}
}
